package cn.edu.ctbu.firstdp.service;

import cn.edu.ctbu.firstdp.dao.TeacherRepository;
import cn.edu.ctbu.firstdp.entity.Student;
import cn.edu.ctbu.firstdp.entity.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TeacherService {
    @Autowired
    public TeacherRepository teacherRepository;

    //查找最大的工号
    public List<Teacher> printMaxPrefixTno()
    {
        List<Teacher> teachers=teacherRepository.findTeacherWithMaxPrefixTno();
        if(!teachers.isEmpty())
        {
            System.out.println("工号最大的教师工号为：");
            for(Teacher teacher:teachers)
            {
                System.out.println(teacher.getTno());
            }
        }
        else {
            System.out.println("未找到教师信息！");
        }
        return teachers;
    }

    //按姓名查找教师
    public List<Teacher> findByName(String name){
        return  teacherRepository.findByNameLike(name);
    }

    //添加老师信息
    public void insert(Teacher teacher)
    {
        teacherRepository.save(teacher);
    }

    //修改教师信息
    public void update(Teacher teacher)
    {
        teacherRepository.save(teacher);
    }

    //删除教师信息
    public void delete(Integer id)
    {
        teacherRepository.deleteById(id);
    }


    /**
     * 读教师列表的所有信息
     * @return List Teacher
     */
    public List<Teacher> getALL(){
        return teacherRepository.findAll();
    }

    /**
     * 按id进行查询
     * @param id 主键,整数
     * @return List Teacher
     */

    public Teacher findById(Integer id){

        return teacherRepository.findById(id).get();
    }
    /**
     * 添加教师
     * @param teacher
     * @return
     */
    public Teacher add(Teacher teacher){
        return teacherRepository.save(teacher);
    }

    /**
     * 更新教师
     * @param teacher
     * @return
     */
    public Teacher update1(Teacher teacher){
        return teacherRepository.save(teacher);
    }

    /**
     * 删除教师
     * @param id
     * @return
     */
    public void delete1(Integer id){
        teacherRepository.deleteById(id);
    }


}
